package com.loongtech.bi.manager.system;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.loongtech.bi.entity.system.EntitySysUser;
import com.loongtech.bi.support.Log;
import com.loongtech.core.jpa.manager.ManagerBase;
import com.loongtech.core.util.Utils;

@Service("systemUserManager")
public class SysUserManager extends ManagerBase<EntitySysUser> {
	private static final long serialVersionUID = 4L;
	private static final String ResetPassword ="123456";
	
	public boolean isAdmin(String username){
		EntitySysUser user = getUserByName(username);
		return null != user && user.getIsAdmin();
	}
	
	public boolean isAdmin(int userId){
		EntitySysUser user = get(userId);
		return null != user && user.getIsAdmin();
	}
	
	/**
	 * 登录判断用户名和密码是否正确
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean login(String username, String password, String time) {
        EntitySysUser user = getUserByName(username);
        if (null == user) {
            Log.bi.info("UserManager login user not exist with name=" + username);
            return false;
        }
        if (null == password || password.isEmpty()) {
            Log.bi.info("SystemUserManager login password error !");
            return false;
        }

        if (StringUtils.isEmpty(time)) {
            return user.getPassword().equals(password);
        } else if ("ys".equals(time)) {
            return user.getPassword().equals(Utils.md5(password));
        } else {
            return password.equals(Utils.md5(username + time + user.getSign()));
        }
	}
 

	/**
	 * 注册用户
	 * 
	 * @return
	 */
	public Integer register(EntitySysUser user) {
		if (null == user) {
			Log.bi.info("SystemUserManager register user is null error !");
			return null;
		}
		String username = user.getUsername();
		if (null == username || username.length() > 255) {
			Log.bi.info("SystemUserManager register username error !");
			return null;
		}
		String password = user.getPassword();
		if (null == password || password.length() > 255) {
			Log.bi.info("SystemUserManager register password error !");
			return null;
		}

		// 检查是否存在
		if (null != getBy(EntitySysUser.K.username, username)) {
			Log.bi.info("SystemUserManager register user exist with name="	+ username);
			return null;
		}
				
		try {
			// 保存入库
			user.setPassword(Utils.md5(password));
			persist(user);
			return user.getId();
		} catch (Exception ex) {
			Log.bi.info("UserManager register Exception {},{}", user,	ex);
			throw new RuntimeException(ex);
		}
	}
	
	/**
	 * 修改密码
	 * @param username
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	public boolean changePassword(String username,String oldPassword,String newPassword){
		EntitySysUser user = getUserByName(username);
		if (null == user) {
			Log.bi.info("UserManager changePassword user not exist with name=" + username);
			return false;
		}
		if (null == newPassword || newPassword.length() > 255) {
			Log.bi.info("SystemUserManager changePassword newPassword error !");
			return false;
		}
		if (null == oldPassword || oldPassword.length() > 255) {
			Log.bi.info("SystemUserManager changePassword oldPassword error !");
			return false;
		}
		if(!Utils.md5(oldPassword).equals(user.getPassword())){
			Log.bi.info("SystemUserManager changePassword oldPassword is not correct !");
			return false;
		}
		try {
			// 保存入库
			user.setPassword(Utils.md5(newPassword));
			merge(user);
			return true;
		} catch (Exception ex) {
			Log.bi.info("SystemUserManager changePassword Exception {},{}", user, ex);
			throw new RuntimeException(ex);
		}
	}

	/**
	 * 重置密码
	 * 
	 * @param userId
	 * @return
	 */
	public boolean resetPassword(int userId) {
		// 检查是否存在
		EntitySysUser user = get(userId);
		if (null == user) {
			Log.bi.info("SystemUserManager resetPassword user not exist with userId=" + userId);
			return false;
		}

		// 修改密码参数
		user.setPassword(Utils.md5(ResetPassword));

		// 保存入库
		try {
			merge(user);
			return true;
		} catch (Exception ex) {
			Log.bi.info("SystemUserManager resetPassword Exception {},{}",	user, ex);
			throw new RuntimeException(ex);
		}
	}

	/**
	 * 重置密码
	 * 
	 * @param username
	 * @return
	 */
	public boolean resetPassword(String username) {
		EntitySysUser user = getUserByName(username);
		if (null == user) {
			Log.bi.info("SystemUserManager resetPassword user not exist with name=" + username);
			return false;
		}

		// 修改密码参数
		user.setPassword(Utils.md5(ResetPassword));

		// 保存入库
		try {
			merge(user);
			return true;
		} catch (Exception ex) {
			Log.bi.info("SystemUserManager resetPassword Exception {},{}",	user, ex);
			throw new RuntimeException(ex);
		}
	}

	/**
	 * 根据名称获得用户
	 * 
	 * @return
	 */
	public EntitySysUser getUserByName(String username) {
		if (null == username || username.length() > 255) {
			Log.bi.info("SystemUserManager getUserByName username error !");
			return null;
		}
		// 检查是否存在
		return getBy(EntitySysUser.K.username, username);
	}
	
	public Integer getUserIdByName(String username){
		EntitySysUser user = getUserByName(username);
		return null != user ? user.getId() : null;
	}
}